package com.infosys.cabplanningdao.driver.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.infosys.cabplanning.common.domain.Driver;
import com.infosys.cabplanningdao.driver.dao.DriverDAO;

public class DriverDAOImpl extends JdbcDaoSupport implements DriverDAO {

	public void save(final List<Driver> driverList) {
		System.out.println(driverList.toString());
		String sql = "INSERT INTO hackathon.driver "
				+ "(dl_no, first_name, last_name, phone) VALUES (?, ?, ?, ?)";

		getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
			 
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				Driver driver = driverList.get(i);
				ps.setString(1, driver.getDrivingLicenseNumber());
				ps.setString(2, driver.getFirstName());
				ps.setString(3, driver.getLastName());
				ps.setString(4, driver.getPhoneNumber());
			}
		 
			@Override
			public int getBatchSize() {
				return driverList.size();
			}
		  });
	}

	/**
	 * 
	 * @param custId
	 * @return
	 */
	public List<Driver> findDriver(Driver driverSearchDTO) {

		String sql = "SELECT driver_id, dl_no, "
				+ "first_name, last_name, phone " + "FROM hackathon.driver WHERE";

		String queryWhereConditions = "";
		List<Object> parameterList = new ArrayList<Object>();
		if (driverSearchDTO.getDriverId() != 0) {
			queryWhereConditions = " driver_id = ? ";
			parameterList.add(driverSearchDTO.getDriverId());
		}
		if (driverSearchDTO.getDrivingLicenseNumber() != null && !driverSearchDTO.getDrivingLicenseNumber().isEmpty()) {
			queryWhereConditions = queryWhereConditions.isEmpty() ? queryWhereConditions : queryWhereConditions + " AND ";
			queryWhereConditions = queryWhereConditions + " dl_no = ?";
			parameterList.add(driverSearchDTO.getDrivingLicenseNumber());
		}
		if (driverSearchDTO.getFirstName() != null && !driverSearchDTO.getFirstName().isEmpty()) {
			queryWhereConditions = queryWhereConditions.isEmpty() ? queryWhereConditions : queryWhereConditions + " AND ";
			queryWhereConditions = queryWhereConditions + "  first_name = ? ";
			parameterList.add(driverSearchDTO.getFirstName());
		}
		if (driverSearchDTO.getLastName() != null && !driverSearchDTO.getLastName().isEmpty()) {
			queryWhereConditions = queryWhereConditions.isEmpty() ? queryWhereConditions : queryWhereConditions + " AND ";
			queryWhereConditions = queryWhereConditions + "  last_name = ?";
			parameterList.add(driverSearchDTO.getLastName());
		}
		if (driverSearchDTO.getPhoneNumber() != null && !driverSearchDTO.getPhoneNumber().isEmpty()) {
			queryWhereConditions = queryWhereConditions.isEmpty() ? queryWhereConditions : queryWhereConditions + " AND ";
			queryWhereConditions = queryWhereConditions + " phone = ?";
			parameterList.add(driverSearchDTO.getPhoneNumber());
		}
		
		if (!parameterList.isEmpty()) {
			System.out.println("Conditions : "+queryWhereConditions);
			sql = sql + queryWhereConditions;
			
			System.out.println("Query : " + sql);
			System.out.println("parameters : " + parameterList);
			
			List<Driver> driverList = getJdbcTemplate().query(sql,
					parameterList.toArray(), new DriverRowMapper());
			return driverList;
		} else {
			//TODO Throw
			return new ArrayList<Driver>();
		}
	}
	
//	public Driver findByLicenseNo(String licenseNumber) {
//
//		String sql = "SELECT driver_id, dl_no, "
//				+ "first_name, last_name, phone " + "FROM DRIVER where dl_no = ?";
//
//		Driver driver = (Driver) getJdbcTemplate().queryForObject(sql,
//				new Object[] { licenseNumber }, new DriverRowMapper());
//
//		return driver;
//
//	}

	/**
	 * 
	 */
	public List<Driver> findAll() {
		String sql = "SELECT driver_id, dl_no, "
				+ "first_name, last_name, phone " + "FROM hackathon.driver";
		List<Driver> driverList = getJdbcTemplate().query(sql,
				new DriverRowMapper());
		return driverList;
	}
}
